package com.hopes.bizz.poll.pollaqi.util;

import com.hopes.bizz.poll.pollaqi.entity.Aqi;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Arrays;

/**
 * @author: cx
 * @create: 2023/9/27
 */

public class WqiUtil {
    /**
     *
     * @Description: 计算wqi值对应的等级
     * @param @param
     *            aqi
     * @param @return
     */
    public static int getPollutionDegree(Double aqi) {
        int pollutionDegree = 1;
        if (aqi <= 50) {
            pollutionDegree = 1;
        } else if (aqi > 50 && aqi <= 100) {
            pollutionDegree = 2;
        } else if (aqi > 100 && aqi <= 150) {
            pollutionDegree = 3;
        } else if (aqi > 150 && aqi <= 200) {
            pollutionDegree = 4;
        } else if (aqi > 200 && aqi <= 300) {
            pollutionDegree = 5;
        } else if (aqi > 300) {
            pollutionDegree = 6;
        }
        return pollutionDegree;
    }

    /**
     *
     * @Description: 计算wqi值对应的等级
     * @param @param
     *            pollutionDegree
     * @param @return
     */
    public static String getDegree(int pollutionDegree) {
        if (pollutionDegree == 1) {
            return "Ⅰ类";
        } else if (pollutionDegree == 2) {
            return "Ⅱ类";
        } else if (pollutionDegree == 3) {
            return "Ⅲ类";
        } else if (pollutionDegree == 4) {
            return "Ⅳ类";
        } else if (pollutionDegree == 5) {
            return "Ⅴ类";
        }
        return "数据错误";
    }

    /**
     * 计算每种污染物项目 P的空气质量分指数
     *
     * @param cp
     *            污染物项目P的质量浓度
     * @param r
     *            污染物项目P所在数组中的行号
     * @return
     */
    public static int countPerIaqi(double cp, int r) {
        int leveal = 0;
        // 空气质量分指数及对应的污染物项目浓度限值
        double[][] aqiArr = { { 6, 6, 6, 6, 9 }, { 0, 0, 0, 0, 0 },{ 15, 15, 20, 30, 40 },
                 { 0.01, 0.05, 0.05, 0.05, 0.1 },{ 0.01, 0.05, 0.05, 0.05, 0.1 },{ 0, 0, 0, 0, 0 },
                 { 0.01, 1.0, 1.0, 1.0, 1.0 },{ 0.2, 0.2, 1.0, 1.5, 2.0 },{ 0.2, 0.2, 1.0, 1.5, 2.0 },
                { 0.02, 0.1, 0.2, 0.3, 0.4}, {0,0,0,0,0},{0,0,0,0,0} };

        double min = aqiArr[r-1][0];
        int  index= aqiArr[r-1].length-1;
        double max = aqiArr[r-1][index];

        for (int j = 0; j < aqiArr[0].length; j++) {
            if (cp <= aqiArr[r-1][j]) {
                leveal = j+1;
                break;
            }
        }
         // 返回水质 对应等级
         return leveal;
    }

    /**
     * 根据提供污染物的各项指标，对AQI进行计算
     *
     * @param PH
     *            pH值
     * @param TONE
     *            色度
     * @param COD
     *            COD
     * @param CR
     *            总铬
     * @param PB
     *            总铅
     * @param NI
     *            总镍
     * @param CU
     *            总铜
     * @param N
     *            总氮
     * @param NH
     *            氨氮
     * @param P
     *            总磷
     * @param CL
     *            氯化物
     * @param FW
     *            污水
     * @return
     */
    public static Aqi CountWqi(double PH, double TONE, double COD, double CR, double PB, double NI, double CU, double N, double NH, double P, double CL, double FW) {
        int phIwqi = getPhIwqi(PH);
        int toneIwqi = getToneIwqi(TONE);
        int codIwqi = getCodIwqi(COD);
        int crIwqi = getCrIwqi(CR);
        int pbIwqi = getPbIwqi(PB);
        int niIwqi = getNiIwqi(NI);
        int cuIwqi = getCuIwqi(CU);
        int nIwqi = getNIwqi(N);
        int nhIwqi = getNhIwqi(NH);
        int pIwqi = getPIwqi(P);
        int clIwqi = getClIwqi(CL);
        int fwIwqi = getFwIwqi(FW);
        // 将变量存入List集合
        List<Integer> iwqiList = Arrays.asList(phIwqi, toneIwqi, codIwqi, crIwqi, pbIwqi, niIwqi, cuIwqi, nIwqi, nhIwqi, pIwqi, clIwqi, fwIwqi);
        // 使用Collections.max方法获取最大值
        double maxIwqi = Collections.max(iwqiList);
        Aqi wqiLeveal = new Aqi("wqiLeveal", maxIwqi);
        return wqiLeveal;
    }




    public static int getPhIwqi(double PH){
        if(PH >= 6 && PH <=9){
            return 0;
        }
        //直接定位等级5
        return 5;

    }
    public static int getToneIwqi(double TONE){
        if(TONE > 0){
            return countPerIaqi(TONE, 2);
        }
        return 0;
    }
    public static int getCodIwqi(double COD){
        if(COD > 0){
            return  countPerIaqi(COD, 3);
        }
        return 0;
    }

    public static int getCrIwqi(double CR){
        if(CR > 0){
            return countPerIaqi(CR,  4);
        }
        return 0;
    }


    public static int getPbIwqi(double PB){
        if(PB > 0 ){
            return countPerIaqi(PB, 5);
        }
        return 0;
    }

    public static int getNiIwqi(double NI){
        if(NI > 0){
            return countPerIaqi(NI, 6);
        }
        return 0;
    }

    public static int  getCuIwqi(double CU){
        if(CU > 0){
            return countPerIaqi(CU,  7);
        }
        return 0;
    }

    public static int  getNIwqi(double N){
        if(N > 0){
            return countPerIaqi(N,  8);
        }
        return 0;
    }

    public static int  getNhIwqi(double NH){
        if(NH > 0){
            return countPerIaqi(NH,  9);
        }
        return 0;
    }

    public static int  getPIwqi(double P){
        if(P > 0){
            return countPerIaqi(P,  10);
        }
        return 0;
    }

    public static int  getClIwqi(double CL){
        if(CL > 0){
            return countPerIaqi(CL,  11);
        }
        return 0;
    }

    public static int  getFwIwqi(double FW){
        if(FW > 0){
            return countPerIaqi(FW,  12);
        }
        return 0;
    }
}

/**
 * 构造分类器类，对AQI对象链表进行排序
 */
class WqiComparator implements Comparator {
    public int compare(Object o1, Object o2) {
        Aqi a1 = (Aqi) o1;
        Aqi a2 = (Aqi) o2;
        double result = a1.getAqi() - a2.getAqi();
        return (int) result;
    }
}
